package com.czk.ssarag.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.rag.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Author: chenzhangkun
 * @CreateTime: 2025-10-31
 * @Description:
 * @Version: 1.0
 */
@RestController
public class RagController {

    @Resource(name = "deepseekChatClient")
    private ChatClient chatClient;

    @Resource
    private VectorStore vectorStore;

    /**
     * rag搜索返回
     *
     * @param question
     * @return
     */
    @GetMapping("/rag")
    public Flux<String> rag(String question) {
        String systemPrompt = "你是一个助手，请根据提供的上下文信息回答问题。";
        RetrievalAugmentationAdvisor retrievalAugmentationAdvisor = RetrievalAugmentationAdvisor.builder()
                .documentRetriever(VectorStoreDocumentRetriever.builder().vectorStore(vectorStore).build())
                .build();
        return chatClient.prompt().system(systemPrompt).user(question).advisors(retrievalAugmentationAdvisor).stream().content();
    }

}
